//
//  XLProfileTextCell.m
//  Mine
//
//  Created by 于鹏 on 2017/8/30.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLProfileTextCell.h"
//#import <Masonry.h>
#import "Masonry.h"
#import "XLProfileTextCellModel.h"

@interface XLProfileTextCell ()
@property (nonatomic, strong)XLCellLabel *titleLabel;
@property (nonatomic, strong)XLCellLabel *detailLabel;
@property (nonatomic, strong)UIView *containerView;

@end

@implementation XLProfileTextCell

+ (XLBaseTableViewCell *)cellWithIdentifier:(NSString *)cellIdentifier tableView:(UITableView *)tableView {
    XLProfileTextCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[XLProfileTextCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}

- (void)setupUI {
    [super setupUI];
    
    
    [self.contentView addSubview:self.containerView];
    [self.containerView addSubview:self.titleLabel];
    [self.containerView addSubview:self.detailLabel];
    
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(0);
        make.left.equalTo(self.contentView.mas_left).offset(XLP_kContainerInset);
        make.right.equalTo(self.contentView.mas_right).offset(-XLP_kContainerInset);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containerView).offset(XLP_kTextLeftNormalMargin);
        make.top.equalTo(self.containerView.mas_top).offset(XLP_kLabelMargin);
    }];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containerView).offset(XLP_kdetailLeftNormalMargin);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(XLP_kLabelMargin);
        make.right.equalTo(self.containerView).offset(-XLP_kdetailLeftNormalMargin);
    }];
    
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    UIBezierPath *path;
    CGFloat _cornerRadius = 8;
    UIView *targetView = self.containerView;
    switch (_roundCornerType) {
        case XLPRoundCornerCellTypeTop: {
            path = [UIBezierPath bezierPathWithRoundedRect:targetView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];
            break;
        }
            
        case XLPRoundCornerCellTypeBottom: {
            path = [UIBezierPath bezierPathWithRoundedRect:targetView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];
            break;
        }
            
        case XLPRoundCornerCellTypeSingleRow: {
            path = [UIBezierPath bezierPathWithRoundedRect:targetView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];
            break;
        }
            
        case XLPRoundCornerCellTypeDefault:
        default: {
            targetView.layer.mask = nil;
            return;
        }
    }
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = targetView.bounds;
    maskLayer.path = path.CGPath;
    targetView.layer.mask = maskLayer;
}

- (void)setupDataModel:(XLBaseCellModel *)model {
    [super setupDataModel:model];
    XLProfileTextCellModel *textModel = (XLProfileTextCellModel *)model;
    self.titleLabel.text = textModel.title;
    self.detailLabel.text = textModel.detailText;
    
    self.titleLabel.font = textModel.titleFont;
    self.titleLabel.textColor = textModel.titleColor;
    
    self.detailLabel.font = textModel.detailFont;
    self.detailLabel.textColor = textModel.detailColor;
    
    
    
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containerView).offset(textModel.isTopGrid ? XLP_kTextLeftTopGrid : XLP_kTextLeftNormalMargin);
        
    }];
}

#pragma mark - Getter
- (UIView *)containerView {
    if (!_containerView) {
        _containerView = [[UIView alloc]init];
        _containerView.backgroundColor = [UIColor whiteColor];
    }
    return _containerView;
}
- (XLCellLabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[XLCellLabel alloc]init];
    }
    return _titleLabel;
}

- (XLCellLabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[XLCellLabel alloc]init];
    }
    return _detailLabel;
}



@end
